#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Author:    Colin
@Github:    https://github.com/opscolin
@DateTime:  2022/9/20 9:32 AM
@File:      generate-instance-yml.py
@Software:  PyCharm
"""


import os
import yaml
from aliyunapi import AliyunAPI
from config import ALI_AK, ALI_SK


aliyun = AliyunAPI(ALI_AK, ALI_SK)

rds = aliyun.rds_list()

instance_file = "instance.yml"

if os.path.exists(instance_file):
    os.remove(instance_file)

for item_list in aliyun.rds_list():
    for item in item_list:
        instance_id = item['DBInstanceId']
        instance_name = f"rds-{item['DBInstanceDescription']}"
        addr = item['ConnectionString']
        instance = {
            instance_name: {
                "id": instance_id,
                "host": addr
            }
        }

        with open('instance.yml', 'a', encoding='utf-8') as f:
            yaml.dump(instance, f)